﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LibrarySolution.ComControl
{
    public partial class UCDataQuery : UserControl
    {
        public UCDataQuery()
        {
            InitializeComponent();

        }

        /// <summary>
        ///  添加查询是执行的事件
        /// </summary>
        public event dataQueryEventHandler DataQuery;    // 原来 事件就是委托的返回值
        public delegate DataTable dataQueryEventHandler();
        private static DataTable baseData;

        /// <summary>
        ///  绑定底层DataGridView数据
        /// </summary>
        public DataTable Datasource
        {
            get { return datasource; }
            set
            {
                if (value != null)
                {
                    UCDataQuery.baseData = value;
                    DtgvBase.DataSource = value;
                    DtgvBase.Refresh();
                }
            }
        }
        private DataTable datasource;

        /// <summary>
        ///  查询的只读字段(String)
        /// </summary>
        public static String Query
        {
            get { return UCDataQuery.query; }
        }
        private static String query;

        /// <summary>
        ///  返回底层DataGritdView的选中行
        /// </summary>
        /// <returns></returns>
        public string[] rowDataBase()
        {
            string[] result;
            if (baseData.Rows.Count == 0) return null;
            DtgvBase.DataSource = baseData;
            result = new string[DtgvBase.Columns.Count];
            for (int i = 0; i < DtgvBase.Columns.Count; i++)
            {
                result[i] = DtgvBase.SelectedRows[0].Cells[i].Value.ToString();
            }
            return result;
        }

        #region 事件
        private void doQuery()
        {
            if (DataQuery != null)
            {
                DtgvQueryResult.DataSource = DataQuery();
            }
        }

        private void BtnCancle_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void TstripMIQuery_Click(object sender, EventArgs e)
        {
            if (DtgvBase.DataSource != null)
            {
                panel1.BringToFront();
                panel1.Visible = true;
                DtgvBase.Focus();
                DtgvQueryResult.DataSource = DtgvBase.DataSource;
            }
        }

        private void TxtGetQueryStr_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (TxtGetQueryStr.Text.Trim() == "")
                {
                    DtgvQueryResult.DataSource = UCDataQuery.baseData;
                    DtgvQueryResult.Refresh();
                    return;
                }
                query = TxtGetQueryStr.Text;
                doQuery();
                TxtGetQueryStr.Text = "";
            }
        }

        private void DtgvQueryResult_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            panel1.Visible = false;
        }

        private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            panel1.Visible = false;
        }
        #endregion

        #region 方法
        //public void HidePanel()
        //{
        //    panel1.Hide();
        //}
        #endregion
    }
}
